package com.googlecode.neuron.server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;

public class UdpServer {

	private static ArrayList<InetAddress> list_address_1 = new ArrayList<InetAddress>();
	private static ArrayList<String> list_port_1 = new ArrayList<String>();
	private static ArrayList<InetAddress> list_address_2 = new ArrayList<InetAddress>();
	private static ArrayList<String> list_port_2 = new ArrayList<String>();
	private static int count = 0;
	public static void main(String[] args) {
		try {
			DatagramSocket udp_socket = new DatagramSocket(2345);
			while(true) {
				byte[] buffer = new byte[1024];
				DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
				udp_socket.receive(packet);
				String msg = new String(packet.getData(), 0, packet.getLength());
				InetAddress inet_2 = packet.getAddress();
				int port_2 = packet.getPort();
				//boolean is_exist = isExist(inet_2);
				
				System.out.println("IP:" + packet.getAddress() + "\tPort:" + packet.getPort());
				String str1[] = msg.split("/");
				String s = str1[1];
				String str2[] = s.split(":");
				InetAddress inet_1 = InetAddress.getByName(str2[0]);
				int port_1 = Integer.parseInt(str2[1]);
				//if (!is_exist) {
					add(inet_1, port_1, inet_2, port_2);
				//}
				reply_1(udp_socket, inet_2, port_2);
				reply_2(udp_socket, inet_2, port_2, "你在NAT上的IP是:" + inet_2.toString() + "你在NAT上的端口是:" + port_2);
				if (count >= 2) {
					sendMessage(udp_socket);
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}
	
	private static void reply_1(DatagramSocket socket,InetAddress inet, int port) {
		String string = "收到";
		byte[] buffer = string.getBytes();
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet, port);
		try {
			socket.send(packet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static void reply_2(DatagramSocket socket,InetAddress inet, int port, String msg) {
		String string = msg;
		byte[] buffer = string.getBytes();
		DatagramPacket packet = new DatagramPacket(buffer, buffer.length, inet, port);
		try {
			socket.send(packet);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*private static boolean isExist(InetAddress inet) {
		for(int i = 0; i < list_address_2.size(); i++) {
			if (!list_address_2.get(i).getAddress().toString().equals((inet.toString()))) {
				return true;
			}
		}
		return false;
	}*/
	
	private static void add(InetAddress inet_1, int port_1, InetAddress inet_2, int port_2) {
		list_address_1.add(inet_1);
		list_port_1.add("" + port_1);
		list_address_2.add(inet_2);
		list_port_2.add("" + port_2);
		count ++;
		System.out.println("添加成功，总数为" + count);
	}
	
	private static void sendMessage(DatagramSocket socket) {
		System.out.println("开始建立连接");
		String str_1 = "<connect>:" + list_address_2.get(1).toString() + ":" + list_port_2.get(1);
		String str_2 = "<connect>:" + list_address_2.get(0).toString() + ":" + list_port_2.get(0);
		byte[] buf_1 = str_1.getBytes();
		byte[] buf_2 = str_2.getBytes();
		DatagramPacket packet_1 = new DatagramPacket(buf_1, buf_1.length, list_address_2.get(0), Integer.parseInt(list_port_2.get(0)));
		DatagramPacket packet_2 = new DatagramPacket(buf_2, buf_2.length, list_address_2.get(1), Integer.parseInt(list_port_2.get(1)));
		try {
			socket.send(packet_1);
			socket.send(packet_2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
	}
}
